package com.sino.androiddesign.customView


import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R

//文字绘制
class TextFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_text, container, false)
    }
}


class CustomTextView : View {

    constructor(context: Context):this(context,null)
    constructor(context: Context, attributeSet: AttributeSet?):super(context,attributeSet)


    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        mPaint.color = Color.RED   //设置画笔颜色
        mPaint.textSize = 70f   //设置文字大小
        mPaint.strokeWidth = 5f  //设置画笔宽度
//        mPaint.style = Paint.Style.FILL   //设置绘图样式
        mPaint.textAlign = Paint.Align.LEFT  //设置文字对齐方式


        //样式设置
//        mPaint.isFakeBoldText = true  //设置是否为粗体文字
        mPaint.isUnderlineText = true  //设置下划线
        // mPaint.textSkewX = (-0.25).toFloat()  //设置字体水平倾斜度
//        mPaint.isStrikeThruText = true  //设置带有删除线效果

//        mPaint.textScaleX = 2f //设置水平拉伸,高度不变

    }

    override fun onDraw(canvas: Canvas?) {

        val text = "Hello World!"

        //绘图样式，设置为填充
        mPaint.style = Paint.Style.FILL
        canvas!!.drawText(text, 10f, 100f, mPaint)

        //绘图样式设置为描边
        mPaint.style = Paint.Style.STROKE
        canvas.drawText(text, 10f, 200f, mPaint)

        //绘图样式设置为填充且描边
        mPaint.style = Paint.Style.FILL_AND_STROKE
        canvas.drawText(text, 10f, 300f, mPaint)

        //设置倾斜度 正负的区别
        mPaint.style = Paint.Style.FILL
        mPaint.textSkewX = 0.25f
        canvas.drawText(text, 10f, 400f, mPaint)

        mPaint.textSkewX = -0.25f
        canvas.drawText(text, 10f, 500f, mPaint)

        //设置水平拉伸
        mPaint.textScaleX = 2f
        canvas.drawText(text, 10f, 600f, mPaint)

        /// 指定 文字位置drawPosText 已经废弃 性能不好 不再使用

        //沿路径绘制
        val cirPath = Path()
        cirPath.addCircle(200f, 800f,100f, Path.Direction.CCW)
        canvas.drawPath(cirPath,mPaint)

        mPaint.color = Color.GREEN
        canvas.drawTextOnPath("hello",cirPath,20f,20f,mPaint)

        //使用系统字体
        val  familyName ="楷体"
        val font = Typeface.create(familyName, Typeface.NORMAL)
        mPaint.typeface = font
        canvas.drawText("你好啊", 100f,1000f,mPaint)


        //使用自定义 字体
        val mgr=context.assets//得到AssetManager
        val typeface= Typeface.createFromAsset(mgr, "123.TTF")//根据路径得到Typeface
        mPaint.typeface = typeface
        canvas.drawText("hello",100f,1200f, mPaint)//两个构造函数


    }
}
